package com.zxl.yjb.activity.home.position;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps2d.model.LatLng;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zxl.yjb.R;
import com.zxl.yjb.adapter.PositionAdapter;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.LocationBean;
import com.zxl.yjb.bean.MessageEvent;
import com.zxl.yjb.utils.LogUtil;
import com.zxl.yjb.utils.SaveBeanUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HistoryPosition extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_page_title)
    TextView tv_page_title;
    @BindView(R.id.rv_position_list)
    RecyclerView rv_position_list;

    private PoiSearch.Query query;// Poi查询条件类
    private PoiSearch poiSearch;// POI搜索

    private RecyclerView.LayoutManager layoutManager;
    private ArrayList<PoiItem> poiItemList = new ArrayList<>();
    private PositionAdapter positionAdapter;

    private double lon;
    private double lat;
    private LocationBean locationBean;
    private LatLonPoint lp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_position_list);
        locationBean = (LocationBean) SaveBeanUtil.getBean(this, Const.LOCATION_LNGLAT);
        if (locationBean!=null)
        {
            lon = locationBean.getLng();
            lat = locationBean.getLat();
        }
        lp = new LatLonPoint(lat,lon);
        ButterKnife.bind(this);
        initView();
        getSearchPosition();
    }

    @Override
    protected void initView() {
        tv_page_title.setText("历史地址");
        layoutManager = new LinearLayoutManager(this);
        rv_position_list.setLayoutManager(layoutManager);
        positionAdapter = new PositionAdapter(R.layout.item_position_search, poiItemList);
        rv_position_list.setAdapter(positionAdapter);
        positionAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (poiItemList!=null)
                {
                    LatLonPoint latLonPoint = poiItemList.get(position).getLatLonPoint();
                    if (latLonPoint!=null)
                    {
                        EventBus.getDefault().post(new MessageEvent("position",new LatLng(latLonPoint.getLatitude(),latLonPoint.getLongitude())));
                        EventBus.getDefault().post(new MessageEvent("finishPotisionSelect"));
                    }

                }
                finish();
            }
        });
    }


    private void getSearchPosition() {
        query = new PoiSearch.Query("", "", "");// 第一个参数表示搜索字符串，第二个参数表示poi搜索类型，第三个参数表示poi搜索区域（空字符串代表全国）
        query.setPageSize(20);// 设置每页最多返回多少条poiitem
        query.setPageNum(0);// 设置查第一页
        query.setCityLimit(true);

        poiSearch = new PoiSearch(this, query);
        poiSearch.setBound(new PoiSearch.SearchBound(lp, 5000, true));
        poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
            @Override
            public void onPoiSearched(PoiResult poiResult, int i) {
                LogUtil.e("aa", "aa");
                poiItemList.clear();
                poiItemList.addAll(poiResult.getPois());
                positionAdapter.notifyDataSetChanged();
            }

            @Override
            public void onPoiItemSearched(PoiItem poiItem, int i) {
                LogUtil.e("aa", "aa");
            }
        });
        poiSearch.searchPOIAsyn();
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
